<?php


namespace App\Services;

use Qcloud\Cos\Client;

class CosService
{
    public function client()
    {

        $cosClient = new Client(
            array(
                'region' => config('system.tencent_cloud.tencent_cos_region'),
                'schema' => 'https', //协议头部，默认为http
                'credentials' => array(
                    'secretId' => config('system.tencent_cloud.tencent_secret_id'),
                    'secretKey' => config('system.tencent_cloud.tencent_secret_key'))));

        return $cosClient;
    }

    public function image($files)
    {
        try {
            $filePath = $files->store('image');
//            Image::load($filePath)->fit($fit, $width, $height)->save();
            $srcPath = storage_path('app\\image\\' . basename($filePath));
            $file = fopen($srcPath, "rb");
            $bucket = config('system.tencent_cloud.tencent_cos_bucket'); //存储桶名称 格式：BucketName-APPID
            $key = 'video/' . date('Ym') . '/' . basename($filePath);
            $result = $this->client()->Upload($bucket, $key, $file);
            @unlink($srcPath);
            return $result['Location'];
        } catch (\Exception $e) {
            return "$e\n";
//            echo "$e\n";
        }

    }

}
